<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false" width="71%" :visible.sync="visible">
    <el-form :model="dataForm" :inline="true" :rules="dataRule" ref="dataForm" label-width="120px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="客户id" prop="userId">
        <el-input v-model="dataForm.userId" :disabled="disabled" placeholder="客户id" clearable/>
      </el-form-item>
      <el-form-item label="客户预约到院时间" prop="appointmentTime">
        <el-date-picker v-model="dataForm.appointmentTime" :disabled="disabled" placeholder="客户预约到院时间" type="datetime" value-format="yyyy-MM-dd HH:mm:SS" clearable/>
      </el-form-item>
      <el-form-item label="是否到院标志" prop="arrived">
        <el-dict code="select" v-model="dataForm.arrived"></el-dict>
      </el-form-item>
      <el-form-item label="就诊id" prop="medicalTreatId">
        <el-input v-model="dataForm.medicalTreatId" :disabled="disabled" placeholder="就诊id" clearable/>
      </el-form-item>
      <el-form-item label="来院时间" prop="arrivedTime">
        <el-date-picker v-model="dataForm.arrivedTime" :disabled="disabled" placeholder="来院时间" type="datetime" value-format="yyyy-MM-dd HH:mm:SS" clearable/>
      </el-form-item>
      <el-form-item label="咨询项目" prop="discipline">
        <el-dict code="discipline" v-model="dataForm.discipline"></el-dict>
      </el-form-item>
      <el-form-item label="预约渠道" prop="channel">
        <el-dict code="channel" v-model="dataForm.channel"></el-dict>
      </el-form-item>
      <el-form-item label="礼品类型" prop="gift">
        <el-dict code="gift" v-model="dataForm.gift"></el-dict>
      </el-form-item>
      <el-form-item label="微信开发渠道来源" prop="weixinCustomerSource">
        <el-dict code="channel" v-model="dataForm.weixinCustomerSource"></el-dict>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dataForm: {
          id: '',
          createTime: '',
          updateTime: '',
          appointmentId: '',
          userId: '',
          appointmentTime: '',
          arrived: '',
          medicalTreatId: '',
          arrivedTime: '',
          appointmentEmployeeId: '',
          department: '',
          discipline: '',
          channel: '',
          gift: '',
          weixinCustomerSource: ''},
        dataRule: {
          userId: [{required: true, message: '客户id不能为空', trigger: 'blur'}],
          channel: [{required: true, message: '预约渠道不能为空', trigger: 'blur'}],
          discipline: [{required: true, message: '咨询项目不能为空', trigger: 'blur'}],
          other: []
        }
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mld/appointment/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.info
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/mld/appointment/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({ message: '操作成功', type: 'success', duration: 1500 })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
